package com.sicau.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sicau.common.core.domain.entity.SysRole;
import com.sicau.common.core.domain.model.LoginUser;
import com.sicau.common.utils.SecurityUtils;
import com.sicau.common.utils.StringUtils;
import com.sicau.system.domain.BLeave;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sicau.common.annotation.Log;
import com.sicau.common.core.controller.BaseController;
import com.sicau.common.core.domain.AjaxResult;
import com.sicau.common.enums.BusinessType;
import com.sicau.system.domain.BSupplement;
import com.sicau.system.service.IBSupplementService;
import com.sicau.common.utils.poi.ExcelUtil;
import com.sicau.common.core.page.TableDataInfo;

/**
 * 补测申请Controller
 * 
 * @author ruoyi
 * @date 2022-03-19
 */
@RestController
@RequestMapping("/system/supplement")
public class BSupplementController extends BaseController
{
    @Autowired
    private IBSupplementService bSupplementService;

    /** 管理员角色权限标识 */
    private static final String SUPER_ADMIN = "admin";

    /**
     * 查询补测申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:supplement:list')")
    @GetMapping("/list")
    public TableDataInfo list(BSupplement bSupplement)
    {
        startPage();
        if (hasRole("teacher")) {
            bSupplement.setTeacherId(SecurityUtils.getUserId());
        }
        List<BSupplement> list = bSupplementService.selectBSupplementList(bSupplement);
        return getDataTable(list);
    }

    /**
     * 导出补测申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:supplement:export')")
    @Log(title = "补测申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BSupplement bSupplement)
    {
        if (hasRole("teacher")) {
            bSupplement.setTeacherId(SecurityUtils.getUserId());
        }
        List<BSupplement> list = bSupplementService.selectBSupplementList(bSupplement);
        ExcelUtil<BSupplement> util = new ExcelUtil<BSupplement>(BSupplement.class);
        util.exportExcel(response, list, "补测申请数据");
    }

    /**
     * 获取补测申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:supplement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bSupplementService.selectBSupplementById(id));
    }

    /**
     * 新增补测申请
     */
    @PreAuthorize("@ss.hasPermi('system:supplement:add')")
    @Log(title = "补测申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BSupplement bSupplement)
    {
        bSupplement.setStudentId(SecurityUtils.getUserId());
        bSupplement.setCreateBy(SecurityUtils.getUsername());
        return toAjax(bSupplementService.insertBSupplement(bSupplement));
    }

    /**
     * 修改补测申请
     */
    @PreAuthorize("@ss.hasPermi('system:supplement:edit')")
    @Log(title = "补测申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BSupplement bSupplement)
    {
        bSupplement.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(bSupplementService.updateBSupplement(bSupplement));
    }

    /**
     * 删除补测申请
     */
    @PreAuthorize("@ss.hasPermi('system:supplement:remove')")
    @Log(title = "补测申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bSupplementService.deleteBSupplementByIds(ids));
    }



    @PreAuthorize("@ss.hasPermi('system:supplement:examine')")
    @Log(title = "补测审核", businessType = BusinessType.UPDATE)
    @PostMapping("/examine")
    public AjaxResult examine(@RequestBody BSupplement bSupplement){
        BSupplement bSupplement_new = new BSupplement();
        bSupplement_new.setId(bSupplement.getId());
        bSupplement_new.setStatus(bSupplement.getStatus());
        return AjaxResult.success(bSupplementService.updateBSupplement(bSupplement_new));
    }


    private boolean hasRole(String role)
    {
        if (StringUtils.isEmpty(role))
        {
            return false;
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getUser().getRoles()))
        {
            return false;
        }
        for (SysRole sysRole : loginUser.getUser().getRoles())
        {
            String roleKey = sysRole.getRoleKey();
            if (roleKey.equals(StringUtils.trim(role)))
            {
                return true;
            }
        }
        return false;
    }
}
